package com.udaos.modules.allot.services;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;

import com.udaos.common.base.Service;
import com.udaos.models.allot.Ud_allot_agreement;
import com.udaos.models.company.Ud_company;
import com.udaos.models.product.Ud_product_type;
import com.udaos.models.school.Ud_school;
import com.udaos.models.sys.Sys_user;
import com.udaos.modules.company.services.UdCompanySchoolService;
import com.udaos.modules.company.services.UdCompanyService;
import com.udaos.modules.product.services.UdProductTypeService;
import com.udaos.modules.school.services.UdSchoolService;
/**
 * 分成比例
 * @author mutaotao
 *
 */
@IocBean(args = {"refer:dao"})
public class UdAllotAgreementService extends Service<Ud_allot_agreement>{
	
	@Inject
	private UdCompanyService companyService;
	@Inject
	private UdSchoolService schoolService;
	@Inject
	private UdProductTypeService productTypeService;
	@Inject
	private UdCompanySchoolService companySchoolService;
	
	public UdAllotAgreementService(Dao dao) {
		super(dao);
	}

	/**
	 * 获取主体名称
	 * @param id
	 * @return
	 */
	public String getCompanyName(String id) {
		Ud_company company=companyService.fetch(id);
		if(null !=company){
			return company.getName();
		}
		return null;
	}
	/**
	 * 获取驾校名称
	 * @param id
	 * @return
	 */
	public String getSchoolName(String id) {
		Ud_school school=schoolService.fetch(id);
		if(null !=school){
			return school.getName();
		}
		return null;
	}

	/**
	 * 获取产品类型名称
	 * @param id
	 * @return
	 */
	public String getProductTypeName(String id) {
		Ud_product_type productType=productTypeService.fetch(id);
		if(null !=productType){
			return productType.getName();
		}
		return null;
	}
	/**
	 * 获取有效的主体列表
	 * @return
	 */
	public List<Ud_company> findCompany() {
		return companyService.find();
	}
	/**
	 * 获取产品类型列表
	 * @return
	 */
	public List<Ud_product_type> findProductTypeList() {
		return productTypeService.query();
	}
	/**
	 * 获取主体开通的驾校
	 * @param company_id
	 * @return
	 */
	public List<Ud_school> chooseSchool(String company_id) {
		return companySchoolService.findSchoolById(company_id);
	}

	public NutMap init(int length, int start, int draw) {
		String sqls="SELECT a.id AS id,c.name AS company_name,g.name AS school_name,p.name AS product_name, a.company_ratio AS company_ratio,a.school_ratio AS school_ratio,a.trainer_ratio AS trainer_ratio"
				+" FROM ud_allot_agreement a,ud_company  c,ud_school g,ud_product_type p"
				+" WHERE a.company_id=c.id AND a.school_id=g.id AND a.product_type =p.id AND a.delFlag=FALSE";
		//获取主体
		Subject subject = SecurityUtils.getSubject();
		Sys_user user = (Sys_user) subject.getPrincipal();
		String company_id=user.getUnit().getCompany_id();
		sqls+=" AND a.company_id='"+company_id+"'";
		
		Sql sql =Sqls.create(sqls);
		return this.data(length, start, draw, sql, sql);
	}

}
